using System;
using System.Windows.Forms;
using System.ComponentModel;
using fuzhu;
using LKU8.shoukuan;

namespace WanJi_Morder_Import
{
    /// <summary>
    /// Excel导入功能测试窗体
    /// </summary>
    public partial class TestExcelImportForm : Form
    {
        private Button btnTest;
        private RichTextBox txtResult;
        private Label lblInfo;
        
        public TestExcelImportForm()
        {
            InitializeComponent();
        }
        
        private void InitializeComponent()
        {
            this.Text = "万吉Excel导入功能测试";
            this.Size = new System.Drawing.Size(600, 400);
            this.StartPosition = FormStartPosition.CenterScreen;
            
            // 创建控件
            lblInfo = new Label();
            lblInfo.Text = "点击按钮测试Excel导入功能：";
            lblInfo.Location = new System.Drawing.Point(20, 20);
            lblInfo.Size = new System.Drawing.Size(300, 20);
            
            btnTest = new Button();
            btnTest.Text = "测试Excel导入";
            btnTest.Location = new System.Drawing.Point(20, 50);
            btnTest.Size = new System.Drawing.Size(120, 30);
            btnTest.Click += BtnTest_Click;
            
            txtResult = new RichTextBox();
            txtResult.Location = new System.Drawing.Point(20, 90);
            txtResult.Size = new System.Drawing.Size(540, 260);
            txtResult.ReadOnly = true;
            txtResult.ScrollBars = RichTextBoxScrollBars.Vertical;
            
            // 添加控件到窗体
            this.Controls.Add(lblInfo);
            this.Controls.Add(btnTest);
            this.Controls.Add(txtResult);
        }

        private void BtnTest_Click(object sender, EventArgs e)
        {
            txtResult.Clear();
            txtResult.AppendText("开始测试Excel导入功能...\n\n");

            try
            {
                // 选择Excel文件
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Title = "选择万吉制造订单Excel文件进行测试";
                ofd.Filter = "Excel files(*.xls,*.xlsx,*.xlsm)|*.xls;*.xlsx;*.xlsm|All files(*.*)|*.*";
                ofd.Multiselect = false;

                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    string filePath = ofd.FileName;
                    txtResult.AppendText(string.Format("选择的文件: {0}\n\n", filePath));

                    // 验证Excel文件格式
                    txtResult.AppendText("步骤1: 验证Excel文件格式...\n");
                    string validationResult = WanJiExcelImportHelper.ValidateExcelFormat(filePath);
                    txtResult.AppendText(string.Format("验证结果: {0}\n\n", validationResult));

                    if (!validationResult.Contains("验证通过"))
                    {
                        txtResult.AppendText("测试失败：Excel格式不符合要求\n");
                        return;
                    }

                    // 获取工作表信息
                    txtResult.AppendText("步骤2: 获取工作表信息...\n");
                    var sheetNames = WanJiExcelImportHelper.GetSheetNames(filePath);
                    txtResult.AppendText(string.Format("工作表数量: {0}\n", sheetNames.Count));
                    foreach (var sheetName in sheetNames)
                    {
                        txtResult.AppendText(string.Format("- {0}\n", sheetName));
                    }
                    txtResult.AppendText("\n");
                    
                    // 选择工作表
                    int selectedSheetIndex = 0;
                    if (sheetNames.Count > 1)
                    {
                        txtResult.AppendText("步骤3: 选择工作表...\n");
                        using (SheetSelectionForm sheetForm = new SheetSelectionForm(sheetNames))
                        {
                            if (sheetForm.ShowDialog(this) == DialogResult.OK)
                            {
                                selectedSheetIndex = sheetForm.SelectedSheetIndex;
                                txtResult.AppendText(string.Format("选择的工作表: {0}\n\n", sheetNames[selectedSheetIndex]));
                            }
                            else
                            {
                                txtResult.AppendText("用户取消选择工作表\n");
                                return;
                            }
                        }
                    }
                    else
                    {
                        txtResult.AppendText(string.Format("使用默认工作表: {0}\n\n", sheetNames[0]));
                    }

                    // 导入数据
                    txtResult.AppendText(string.Format("步骤{0}: 导入数据...\n", sheetNames.Count > 1 ? 4 : 3));
                    BindingList<WanJi_Morder_View> dataList = WanJiExcelImportHelper.ImportFromExcel(filePath, selectedSheetIndex);
                    txtResult.AppendText(string.Format("成功导入 {0} 条记录\n\n", dataList.Count));
                    
                    // 显示前几条数据
                    txtResult.AppendText(string.Format("步骤{0}: 显示前5条数据...\n", sheetNames.Count > 1 ? 5 : 4));
                    int displayCount = Math.Min(5, dataList.Count);
                    for (int i = 0; i < displayCount; i++)
                    {
                        var item = dataList[i];
                        txtResult.AppendText(string.Format("记录 {0}:\n", i + 1));
                        txtResult.AppendText(string.Format("  序号: {0}\n", item.cRowNo));
                        txtResult.AppendText(string.Format("  工单号: {0}\n", item.cMoCode));
                        txtResult.AppendText(string.Format("  客户: {0}\n", item.cCusName));
                        txtResult.AppendText(string.Format("  系列: {0}\n", item.cXiLie));
                        txtResult.AppendText(string.Format("  工序: {0}\n", item.cOperation));
                        txtResult.AppendText(string.Format("  机种名称: {0}\n", item.cJiZhongName));
                        txtResult.AppendText(string.Format("  订单号: {0}\n", item.cCusOrderCode));
                        txtResult.AppendText(string.Format("  订单日期: {0}\n", item.dDate.ToString("yyyy-MM-dd")));
                        txtResult.AppendText(string.Format("  订单批量: {0}\n", item.iQty));
                        txtResult.AppendText(string.Format("  档位: {0}\n", item.cDangWei));
                        txtResult.AppendText(string.Format("  备注: {0}\n", item.cMemo));
                        txtResult.AppendText(string.Format("  SMT编码: {0}\n", item.cInvCodeSMT));
                        txtResult.AppendText(string.Format("  DIP编码: {0}\n", item.cInvCodeDIP));
                        txtResult.AppendText(string.Format("  后道编码: {0}\n", item.cInvCodeHou));
                        txtResult.AppendText("\n");
                    }

                    txtResult.AppendText("测试完成：Excel导入功能正常！\n");
                }
            }
            catch (Exception ex)
            {
                txtResult.AppendText(string.Format("测试失败：{0}\n", ex.Message));
                txtResult.AppendText(string.Format("详细错误：{0}\n", ex.ToString()));
            }
        }
    }
}